package com.example.opengldemo._026_;

import android.content.Context;
import android.opengl.GLES30;
import android.opengl.GLSurfaceView;


import com.example.opengldemo.util.MatrixState;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

public class _026_MySurfaceView extends GLSurfaceView  implements GLSurfaceView.Renderer {
    BallDemo ballDemo ;

    public _026_MySurfaceView(Context context) {
        super(context);
        initConfigAndRenderer() ;

    }

    private void initConfigAndRenderer() {
        this.setEGLContextClientVersion(3);
        setRenderer(this);
        this.setRenderMode(GLSurfaceView.RENDERMODE_CONTINUOUSLY);
    }

    @Override
    public void onSurfaceCreated(GL10 gl10, EGLConfig eglConfig) {
        GLES30.glClearColor(0.5f, 0.5f, 0.5f, 1f);
        GLES30.glEnable(GLES30.GL_DEPTH_TEST);
        ballDemo = new BallDemo(this) ;
        MatrixState.setInitStack();
        MatrixState.setLightLocation(0, 0, 1.5f); //设置光源位置
        MatrixState.setLightDirection( 0 , 0, 1.5f); //设置光源位置

    }

    @Override
    public void onSurfaceChanged(GL10 gl10, int i, int i1) {
        GLES30.glViewport(0, 0, i, i1);
        float ratio = (float) i / i1;
        MatrixState.setProjectFrustum(-ratio, ratio, -1, 1, 1, 10);
        MatrixState.setCamera(0, 0, 2f, 0, 0, 0f, 0f, 1.0f, 0.0f);
    }

    @Override
    public void onDrawFrame(GL10 gl10) {

        GLES30.glClear(GLES30.GL_DEPTH_BUFFER_BIT | GLES30.GL_COLOR_BUFFER_BIT);

        MatrixState.pushMatrix();
        MatrixState.pushMatrix();
        MatrixState.translate(-1.2f, 0, 0);
        ballDemo.drawSelf();
        MatrixState.popMatrix();
        MatrixState.pushMatrix();
        MatrixState.translate(1.2f, 0, 0);
        ballDemo.drawSelf();
        MatrixState.popMatrix();
        MatrixState.popMatrix();
    }
}
